<?php

class vtnsRequest extends sfWebRequest
{
    /**
     * Returns true if the request is a XMLHttpRequest.
     *
     * It works if your JavaScript library set an X-Requested-With HTTP header.
     * Works with Prototype, Mootools, jQuery, and perhaps others.
     *
     * @return bool true if the request is an XMLHttpRequest, false otherwise
     */
    public function isXmlHttpRequest()
    {
        if ($this->getParameter('is_ajax'))
            return true;
        else
            return ($this->getHttpHeader('X_REQUESTED_WITH') == 'XMLHttpRequest');
    }


    /**
     * Parses additional parameters
     *
     * @return array An array of request parameters.
     */
    protected function parseRequestParameters()
    {
        $parameters = parent::parseRequestParameters();
        $match = array();
        if (preg_match('#^/([as])/(\d+)#', $this->getPathInfo(), $match)) {
            $parameters['fb_secure'] = $match[1];
            $parameters['fb_api_id'] = $match[2];
            sfContext::getInstance()->getLogger()->debug("W4U request secure: {$match[1]} api_id: {$match[2]}");
        }
        return $parameters;
    }


    /**
     * Returns true if the User-Agent indicates any version of Internet Explorer,
     * wich forces us to switch the app to a degraded mode.
     */
    public function isIE()
    {
        return (bool) preg_match('/MSIE /i', $this->getHttpHeader('User-Agent'));
    }


    /**
     * Returns true if the User-Agent indicates Internet Explorer version 6 or 7,
     * wich forces us to switch the app to an even more degraded mode.
     */
    public function isIE67()
    {
        return (bool) preg_match('/MSIE [67]/i', $this->getHttpHeader('User-Agent'));
    }


    /**
     * Returns true if the User-Agent indicates Internet Explorer version 6,
     * wich forces us to switch the app to an even more degraded mode.
     */
    public function isIE6()
    {
        return (bool) preg_match('/MSIE 6/i', $this->getHttpHeader('User-Agent'));
    }


    /**
     * Returns true if the request is made by Facebook to get the properties of
     * a link (i.e. reading the OpenGraph meta-tags instead of displaying the page)
     */
    public function isFacebookExternalHit()
    {
        return (bool) preg_match('/facebookexternalhit/i', $this->getHttpHeader('User-Agent'));
    }
}

